<template>
	<div class="edit-popup">
		<popup ref="popupRef" :title="popupTitle" :async="true" width="650px" @confirm="handleSubmit" @close="handleClose">
			<el-form ref="formRef" :model="formData" label-width="160px" :rules="formRules">
				<el-form-item label="名称" prop="name">
					<el-input v-model="formData.name" placeholder="请输入分类名称" clearable />
				</el-form-item>
				<el-form-item label="icon" prop="icon">
					<div class="w-80">
						<div>
							<material-picker v-model="formData.icon" :limit="1" />
						</div>
						<div class="form-tips">支持扩展名：.jpg .jpeg .png .bmp</div>
					</div>
				</el-form-item>
				<el-form-item label="平台收取服务费(%)" prop="point">
					<el-input v-model="formData.point" placeholder="请输入平台收取服务费(%)" clearable />
				</el-form-item>
				<el-form-item label="保证金" prop="bond_price">
					<el-input v-model="formData.bond_price" placeholder="请输入保证金" clearable />
				</el-form-item>
				<el-form-item label="备注" prop="remark">
					<el-input v-model="formData.remark" type="textarea" :rows="2" placeholder="请输入备注" clearable />
				</el-form-item>
				<el-form-item label="启用状态" prop="is_enable" required>
					<el-radio-group v-model="formData.is_enable">
						<el-radio :label="1">启用</el-radio>
						<el-radio :label="0">停用</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
		</popup>
	</div>
</template>
<script lang="ts" setup>
	import type { FormInstance } from 'element-plus'
	import { saveCate } from '@/api/service';
	import Popup from '@/components/popup/index.vue'
	const emit = defineEmits(['success', 'close'])
	const formRef = shallowRef<FormInstance>()
	const popupRef = shallowRef<InstanceType<typeof Popup>>()
	const mode = ref('add')
	const popupTitle = computed(() => {
		return mode.value == 'edit' ? '编辑分类' : '新增分类'
	})
	
	const formData = reactive({
		id: '',
		name: '',
		icon: '',
		remark: '',
		is_enable: 1,
		point: '',
		bond_price: ''
	})
	
	const formRules = {
		name: [
			{
				required: true,
				message: '请输入名称',
				trigger: 'blur'
			}
		],
		icon: [
			{
				required: true,
				message: '请上传icon',
				trigger: 'blur'
			}
		],
		point: [
			{
				required: true,
				message: '请输入平台收取服务费',
				trigger: 'blur'
			}
		],
		bond_price: [
			{
				required: true,
				message: '请输入保证金',
				trigger: 'blur'
			}
		],
	}
	
	const handleSubmit = async () => {
		await formRef.value?.validate()
		let newFormData = JSON.parse(JSON.stringify(formData));
		mode.value == 'edit' ? await saveCate(newFormData) : await saveCate(newFormData)
		popupRef.value?.close()
		emit('success')
	}

	const open = (type = 'add') => {
		mode.value = type
		popupRef.value?.open()
	}
	
	const setFormData = (data : Record<any, any>) => {
		for (const key in formData) {
			if (data[key] != null && data[key] != undefined) {
				//@ts-ignore
				formData[key] = data[key]
			}
		}
	}

	const getDetail = async (row : Record<string, any>) => {
		setFormData(row)
	}

	const handleClose = () => {
		emit('close')
	}

	defineExpose({
		open,
		setFormData,
		getDetail
	})
</script>

<style scoped lang="scss"></style>